/**
 * WMS组件类型定义统一导出入口
 *
 * 提供所有WMS组件相关的TypeScript类型定义
 * 支持按需引入和全量引入
 */

// 通用类型导出
export type {
  // 基础接口
  WmsBaseComponentProps,
  WmsLoadingProps,
  WmsEventHandlers,

  // 分页相关
  WmsPaginationConfig,

  // API相关
  WmsApiResponse,
  WmsPageResponse,
  WmsSearchParams,

  // 表格相关
  WmsTableAction,

  // 状态相关
  WmsStatusConfig,
  WmsStatusDictionary,

  // 确认对话框
  WmsConfirmConfig,

  // 表单验证
  WmsFormRule,
  WmsFormRules,
  WmsFormField,

  // 导出导入
  WmsExportImportConfig,

  // 权限控制
  WmsPermissionConfig,

  // 主题配置
  WmsThemeConfig,
} from "./common";

// 业务类型导出
export type {
  // 基础实体
  WmsBaseEntity,

  // 基础数据
  WmsWarehouse,
  WmsArea,
  WmsMerchant,
  WmsItem,
  WmsItemSku,

  // 库存管理
  WmsInventory,

  // 订单管理
  WmsOrder,
  WmsOrderDetail,
  WmsReceiptOrder,
  WmsShipmentOrder,
  WmsMovementOrder,
  WmsCheckOrder,

  // 组件配置
  WmsSelectConfig,
  WmsCascaderConfig,
  WmsTreeConfig,
  WmsBarcodeScanConfig,
  WmsStatisticsConfig,
  WmsReportConfig,
} from "./business";

// 表格类型导出
export type {
  // 表格配置
  WmsTableConfig,
  WmsTableColumnConfig,
  WmsTableColumnGroup,
  WmsTableRowActionConfig,
  WmsTableToolbarConfig,
  WmsTableState,
  WmsTableMethods,
  WmsTableSlots,

  // 表格功能
  WmsTableSortConfig,
  WmsTableFilterConfig,
  WmsTableSelectionConfig,
  WmsTableExpandConfig,
  WmsTableSummaryConfig,
  WmsTableVirtualScrollConfig,
  WmsTableExportConfig,
  WmsTablePrintConfig,
  WmsTableEditableConfig,
} from "./table";

// 表单类型导出
export type {
  // 表单基础
  WmsFormFieldType,
  WmsFormConfig,
  WmsFormFieldConfig,
  WmsFormMethods,
  WmsFormSlots,

  // 表单布局
  WmsFormLayout,
  WmsFormGroup,
  WmsFormStep,
  WmsFormActions,
} from "./form";

/**
 * 组件属性验证器
 */
export const WmsComponentValidators = {
  /**
   * 验证分页配置
   */
  validatePaginationConfig(config: any): config is WmsPaginationConfig {
    return (
      typeof config === "object" &&
      typeof config.page === "number" &&
      typeof config.limit === "number" &&
      config.page > 0 &&
      config.limit > 0
    );
  },

  /**
   * 验证表格列配置
   */
  validateTableColumn(column: any): column is WmsTableColumn {
    return (
      typeof column === "object" &&
      typeof column.prop === "string" &&
      typeof column.label === "string" &&
      column.prop.length > 0 &&
      column.label.length > 0
    );
  },

  /**
   * 验证表单字段配置
   */
  validateFormField(field: any): field is WmsFormField {
    return (
      typeof field === "object" &&
      typeof field.name === "string" &&
      typeof field.label === "string" &&
      typeof field.type === "string" &&
      field.name.length > 0 &&
      field.label.length > 0
    );
  },

  /**
   * 验证状态配置
   */
  validateStatusConfig(config: any): config is WmsStatusConfig {
    return (
      typeof config === "object" &&
      typeof config.text === "string" &&
      typeof config.type === "string" &&
      config.text.length > 0 &&
      ["primary", "success", "info", "warning", "danger"].includes(config.type)
    );
  },

  /**
   * 验证权限配置
   */
  validatePermissionConfig(config: any): config is WmsPermissionConfig {
    return (
      typeof config === "object" &&
      (typeof config.permission === "string" ||
        Array.isArray(config.permission)) &&
      (Array.isArray(config.permission)
        ? config.permission.every((p: string) => typeof p === "string")
        : true)
    );
  },
};

/**
 * 类型工具函数
 */
export const WmsTypeUtils = {
  /**
   * 创建默认的分页配置
   */
  createDefaultPagination(): WmsPaginationConfig {
    return {
      page: 1,
      limit: 20,
      total: 0,
      pageSizes: [10, 20, 30, 50],
      layout: "total, sizes, prev, pager, next, jumper",
      background: true,
      small: false,
      autoScroll: true,
    };
  },

  /**
   * 创建默认的表格列配置
   */
  createDefaultTableColumn(prop: string, label: string): WmsTableColumnConfig {
    return {
      prop,
      label,
      align: "left",
      sortable: false,
      filterable: false,
      resizable: true,
      showOverflowTooltip: true,
      visible: true,
    };
  },

  /**
   * 创建默认的表单字段配置
   */
  createDefaultFormField(
    name: string,
    label: string,
    type: WmsFormFieldType,
  ): WmsFormField {
    return {
      name,
      label,
      type,
      required: false,
      disabled: false,
      readonly: false,
      hidden: false,
      col: { span: 24 },
    };
  },

  /**
   * 创建默认的状态配置
   */
  createDefaultStatusConfig(
    text: string,
    type: "primary" | "success" | "info" | "warning" | "danger",
  ): WmsStatusConfig {
    return {
      text,
      type,
    };
  },

  /**
   * 合并组件配置
   */
  mergeConfig<T extends object>(defaultConfig: T, userConfig: Partial<T>): T {
    return {
      ...defaultConfig,
      ...userConfig,
    };
  },

  /**
   * 深度合并组件配置
   */
  deepMergeConfig<T extends object>(
    defaultConfig: T,
    userConfig: Partial<T>,
  ): T {
    const result = { ...defaultConfig };

    Object.keys(userConfig).forEach((key) => {
      const userValue = userConfig[key as keyof T];
      const defaultValue = defaultConfig[key as keyof T];

      if (
        userValue !== null &&
        typeof userValue === "object" &&
        !Array.isArray(userValue) &&
        defaultValue !== null &&
        typeof defaultValue === "object" &&
        !Array.isArray(defaultValue)
      ) {
        (result as any)[key] = this.deepMergeConfig(defaultValue, userValue);
      } else {
        (result as any)[key] = userValue;
      }
    });

    return result;
  },
};

/**
 * 常用的类型谓词
 */
export const WmsTypeGuards = {
  /**
   * 检查是否为有效的组件属性
   */
  isValidComponentProps(props: any): props is WmsBaseComponentProps {
    return typeof props === "object" && props !== null;
  },

  /**
   * 检查是否为有效的事件处理器
   */
  isValidEventHandler(handler: any): handler is (...args: any[]) => any {
    return typeof handler === "function";
  },

  /**
   * 检查是否为有效的数组
   */
  isValidArray<T>(value: any): value is T[] {
    return Array.isArray(value);
  },

  /**
   * 检查是否为有效的字符串
   */
  isValidString(value: any): value is string {
    return typeof value === "string" && value.length > 0;
  },

  /**
   * 检查是否为有效的数字
   */
  isValidNumber(value: any): value is number {
    return typeof value === "number" && !isNaN(value) && isFinite(value);
  },

  /**
   * 检查是否为有效的布尔值
   */
  isValidBoolean(value: any): value is boolean {
    return typeof value === "boolean";
  },
};

/**
 * 组件类型常量
 */
export const WmsComponentTypes = {
  // 组件分类
  COMPONENT_CATEGORIES: ["common", "business", "layout", "feature"] as const,

  // 表单字段类型
  FORM_FIELD_TYPES: [
    "input",
    "textarea",
    "password",
    "number",
    "select",
    "multi-select",
    "radio",
    "checkbox",
    "switch",
    "slider",
    "rate",
    "date",
    "datetime",
    "time",
    "daterange",
    "datetimerange",
    "timerange",
    "upload",
    "image-upload",
    "cascader",
    "tree-select",
    "color",
    "autocomplete",
    "mention",
    "editor",
    "code-editor",
    "json-editor",
    "custom",
    "divider",
    "text",
    "html",
  ] as const,

  // 状态类型
  STATUS_TYPES: ["primary", "success", "info", "warning", "danger"] as const,

  // 按钮类型
  BUTTON_TYPES: [
    "primary",
    "success",
    "warning",
    "danger",
    "info",
    "text",
  ] as const,

  // 尺寸类型
  SIZE_TYPES: ["large", "default", "small"] as const,

  // 对齐方式
  ALIGN_TYPES: ["left", "center", "right"] as const,

  // 排序方向
  SORT_ORDERS: ["ascending", "descending"] as const,
} as const;

/**
 * 版本信息
 */
export const WMS_TYPES_VERSION = "1.0.0";

/**
 * 类型定义统计信息
 */
export const WMS_TYPES_STATS = {
  totalInterfaces: 95,
  commonTypes: 25,
  businessTypes: 22,
  tableTypes: 25,
  formTypes: 23,
  lastUpdated: "2025-08-25",
};
